//
//  InstallationChecklistTableView.swift
//  ezyform
//
//  Created by MoennigQ on 8/9/21.
//  Copyright © 2021 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxRelay
import RxDataSources

class InstallationChecklistTableView: BaseChecklistTableView {
    //MARK: - 绑定pvd checklist 数据源
    override func bindData() {
        self.register(PVDDetailChecklistSelfieCell.self, forCellReuseIdentifier: PVDDetailChecklistSelfieCell.reuseCellIdentifier)
        self.register(PVDDetailChecklistScanCell.self, forCellReuseIdentifier: PVDDetailChecklistScanCell.reuseCellIdentifier)
        self.register(ChecklistHeaderWithSectionImageCell.self, forCellReuseIdentifier: ChecklistHeaderWithSectionImageCell.reuseCellIdentifier)
        self.register(PVDDetailChecklistHeaderCell.self, forCellReuseIdentifier: PVDDetailChecklistHeaderCell.reuseCellIdentifier)
        self.register(PVDDetailChecklistTableViewCell.self, forCellReuseIdentifier: PVDDetailChecklistTableViewCell.reuseCellIdentifier)
        self.register(PVDDetailSignatureTableViewCell.self, forCellReuseIdentifier: PVDDetailSignatureTableViewCell.reuseCellIdentifier)
        self.register(PVDDetailChecklistCertificatedCell.self, forCellReuseIdentifier: PVDDetailChecklistCertificatedCell.reuseCellIdentifier)
        //绑定数据源，自动刷新数据
        JobDetailModelHelper.shared.pvd_checklists.bind(to: self.rx.items){(tableView, row, checkModel)-> UITableViewCell in
            
            if checkModel.type == ChecklistType.header.rawValue{//header cell
                let cell = PVDDetailChecklistHeaderCell(style: .default, reuseIdentifier: PVDDetailChecklistHeaderCell.reuseCellIdentifier)
                cell.setData(checkModel)
                return cell
            }else if checkModel.type == ChecklistType.headerWithImage.rawValue{//header cell with image
                let cell = ChecklistHeaderWithSectionImageCell(style: .default, reuseIdentifier: ChecklistHeaderWithSectionImageCell.reuseCellIdentifier)
                cell.setData(checkModel.title, section: checkModel.section)
                return cell
            }else if  checkModel.id == ChecklistFixedId.preInstall.rawValue || checkModel.id == ChecklistFixedId.midInstall.rawValue || checkModel.id == ChecklistFixedId.testAndCommission.rawValue {//三个自拍照的cell
                let cell = PVDDetailChecklistSelfieCell(style: .default, reuseIdentifier: PVDDetailChecklistSelfieCell.reuseCellIdentifier)
                cell.setData(JobDetailModelHelper.shared.jobDetailModel,index: row)
                return cell
            }else if checkModel.section == CheckListSectionType.scan{//panes , inverter cell
                let cell = PVDDetailChecklistScanCell(style: .default, reuseIdentifier: PVDDetailChecklistScanCell.reuseCellIdentifier)
                cell.setData(checkModel)
                return cell
            }else  {//其他是标识为4或6或7或8的section
                //1.是不是此section的第一行，默认是
                var firstRowOfSectionFlag: Bool = true
                if row >  0{
                    let preRowModel = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[row - 1]
                    if preRowModel.section == checkModel.section && preRowModel.type != ChecklistType.header.rawValue && preRowModel.type != ChecklistType.headerWithImage.rawValue {//如果相等表示不是第一行
                        firstRowOfSectionFlag = false
                    }
                }
                //2.是不是此section的最后一行，默认是
                var lastRowOfSectionFlag: Bool = true
                if row < (JobDetailModelHelper.shared.jobDetailModel.pvd_checklists.count - 1){
                    let lastRowModel = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[row + 1]
                    if lastRowModel.section == checkModel.section {//如果相等表示不是第一行
                        lastRowOfSectionFlag = false
                    }
                }
                if checkModel.section == .certificates{
                    let cell = PVDDetailChecklistCertificatedCell(style: .default, reuseIdentifier: PVDDetailChecklistCertificatedCell.reuseCellIdentifier)
                    cell.setData(checkModel, lastRowFlag: lastRowOfSectionFlag, firstRowFlag: firstRowOfSectionFlag)
                    return cell
                }else if checkModel.section == .signatures{
                    let cell = PVDDetailSignatureTableViewCell(style: .default, reuseIdentifier: PVDDetailSignatureTableViewCell.reuseCellIdentifier)
                    cell.setData(checkModel, lastRowFlag: lastRowOfSectionFlag)
                    return cell
                }else{//section是otherAttachment, otherChecklist的cell
                    let cell = PVDDetailChecklistTableViewCell(style: .default, reuseIdentifier: PVDDetailChecklistTableViewCell.reuseCellIdentifier)
                    cell.setData(checkModel, firstRowFlag: firstRowOfSectionFlag,lastRowFlag: lastRowOfSectionFlag)
                    return cell
                }
            }
        }.disposed(by: self.disposeBag)
    }
    override func operateChecklist(_ row: Int) {
        if JobDetailModelHelper.shared.jobDetailModel.solar_water_heater == false{//PVD
            let panelsPlantCnt = JobDetailModelHelper.shared.jobDetailModel.system_detail.panels.count
            if panelsPlantCnt == 0{//没有任何panel数据，给与提示
                Tool().showAlertViewWithOneBtn(message: R.string.localizable.pleaseSelectPVModelFirst())
                return
            }
        }
        let serverModel = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists[row]
        let type = serverModel.type
        if type == ChecklistType.DerRegister.rawValue{//der register
            self.toDERRegisterClosure?()
        }else if type == ChecklistType.PanelInstalled.rawValue {//panel installed
            //没有panel installed
            if !JobDetailModelHelper.shared.jobDetailModel.spv_installed {
                self.toPanelInstallClosure?()
            }
        }else if serverModel.type == ChecklistType.Bool.rawValue {
            self.toClickChecklitBoolItemClosure?(row)
        }else if serverModel.type == ChecklistType.Image.rawValue{//image,noc，coc，other attachment
            self.toImageListClosure?(row)
        }else if serverModel.type == ChecklistType.Text.rawValue  {
            self.toEditTextClosure?(serverModel)
        }else if serverModel.section == .signatures && serverModel.type != ChecklistType.header.rawValue{//sinatures
            guard let signType = SignatureType(rawValue: serverModel.type) 
            else {
                return
            }
            self.toSignatureClosure?(signType)
        }else if serverModel.id == ChecklistFixedId.inverters.rawValue {//inverters
            self.toInverterListClickClosure?()
        }else if serverModel.id == ChecklistFixedId.panels.rawValue{
            self.toPanelListClickClosure?()
        }else if serverModel.id == ChecklistFixedId.tanks.rawValue{
            self.toTankListClickClosure?()
        }
    }
}
